package com.caigou.modules.personal.ui.info

import android.os.Bundle
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.container.BaseFragment
import com.caigou.modules.personal.databinding.ActivityPersonalInfoBinding
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

/**
 * @author #Suyghur.
 * Created on 2021/09/30
 */
@Route(path = RouterPath.Personal.PAGE_PERSONAL_INFO)
@AndroidEntryPoint
class PersonalInfoActivity : BaseActivity() {

    private lateinit var binding: ActivityPersonalInfoBinding
    private var infoFragment: BaseFragment? = null
    private var modifyPwdFragment: BaseFragment? = null
    private var nickNameFragment: BaseFragment? = null
    private var realNameFragment: BaseFragment? = null
    private var signatureFragment: BaseFragment? = null
    private var levelFragment: BaseFragment? = null
    private var growthVoFragment: BaseFragment? = null

    @Autowired(name = AppConstants.TARGET_FRAGMENT_TAG)
    lateinit var targetFragmentTag: String


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPersonalInfoBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.infoFlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)
        switchFragment(targetFragmentTag)
    }

    override fun switchFragment(tag: String) {
        when (tag) {
            RouterPath.Personal.TAG_INFO -> {
                if (infoFragment == null) {
                    infoFragment = InfoMainFragment()
                }
                switchFragment(infoFragment!!, tag)
            }
            RouterPath.Personal.TAG_MODIFY -> {
                if (modifyPwdFragment == null) {
                    modifyPwdFragment = ModifyPwdFragment()
                }
                switchFragment(modifyPwdFragment!!, tag)
            }
            RouterPath.Personal.TAG_NICK_NAME -> {
                if (nickNameFragment == null) {
                    nickNameFragment = NickNameFragment()
                }
                switchFragment(nickNameFragment!!, tag)
            }
            RouterPath.Personal.TAG_REAL_NAME -> {
                if (realNameFragment == null) {
                    realNameFragment = RealNameFragment()
                }
                switchFragment(realNameFragment!!, tag)
            }
            RouterPath.Personal.TAG_SIGNATURE -> {
                if (signatureFragment == null) {
                    signatureFragment = SignatureFragment()
                }
                switchFragment(signatureFragment!!, tag)
            }
            RouterPath.Personal.TAG_LEVEL -> {
                if (levelFragment == null) {
                    levelFragment = LevelFragment()
                }
                switchFragment(levelFragment!!, tag)
            }
            RouterPath.Personal.TAG_GROWTH_VALUE -> {
                if (growthVoFragment == null) {
                    growthVoFragment = GrowthVoFragment()
                }
                switchFragment(growthVoFragment!!, tag)
            }
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        ev?.let {
            if (it.action == MotionEvent.ACTION_DOWN) {
                val v = currentFocus
                if (isShouldHideInput(v, it)) {
                    val imm = this@PersonalInfoActivity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(v!!.windowToken, 0)
                }
                return super.dispatchTouchEvent(it)
            }
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        return if (window.superDispatchTouchEvent(ev)) {
            true
        } else onTouchEvent(ev)
    }

    override fun onBackPressed() {
        if (fragmentStack.isNotEmpty() && fragmentStack.size > 1) {
            when (fragmentStack.last) {
                RouterPath.Personal.TAG_MODIFY -> modifyPwdFragment?.onBack()
                RouterPath.Personal.TAG_NICK_NAME -> nickNameFragment?.onBack()
                RouterPath.Personal.TAG_REAL_NAME -> realNameFragment?.onBack()
                RouterPath.Personal.TAG_SIGNATURE -> signatureFragment?.onBack()

            }
            fragmentStack.removeLast()
            switchFragment(fragmentStack.last)
        } else {
            super.onBackPressed()
        }
    }
}